# -----------------------------------------------------------------------------
# MAKEFILE for LaTeX
#
# V1.1
#
# Last Modification: 23/12/2005 by Gullaume Goussard
# Modification:      13/05/2003 by Gurvan Huiban
# -----------------------------------------------------------------------------

# -----------------------------------------------------------------------------
# Compilers
# ---------

# The latex software
COMPPS=latex

# The pdflatex software
COMPPDF=pdflatex

# The bibtex software
COMPBIB=bibtex

# The index Software
COMPIDX=makeindex

# -----------------------------------------------------------------------------
# Viewers
# ---------
PDFVIEW=xpdf

# -----------------------------------------------------------------------------
# Preferences
# -----------

# Do you want to produce a .ps.gz file from you .tex file with latex ?
# set it to yes or write nothing.
#CREATE_PS_GZ=yes
CREATE_PS_GZ=

# Do you want to produce a .ps file from you .tex file with latex ?
# set it to yes or write nothing.
#CREATE_PS=yes
CREATE_PS=

# Do you want to produce a .pdf file from you .tex file with pdflatex ?
# set it to yes or write nothing.
CREATE_PDF=yes
#CREATE_PDF=

# Do you want to clean the environnement ? (.aux, .dvi, .log, .blg, ...)
# set it to yes or write nothing.
CLEAN_TEX=yes

# The string which is used in your tex file to include the bibliography
BIBLIO_TEX=^[[:space:]]*[\]bibliography{

# -----------------------------------------------------------------------------
# The rules
# ---------

# PHONY
.PHONY : clean all tex

# To compile all .tex files and generated postricript and pdf
tex: $(if $(CREATE_PS_GZ),$(subst .tex,.ps.gz, $(wildcard ./*.tex)),) $(if $(CREATE_PDF),$(subst .tex,.pdf, $(wildcard ./*.tex)),) $(if $(CREATE_PS),$(subst .tex,.ps, $(wildcard ./*.tex)),)
	$(MAKE) $^
	if test -n "$(CLEAN_TEX)"; then $(MAKE) $(subst .tex,.tex_clean, $(wildcard ./*.tex)); fi;

# This file is generated by the Makefile
# What is funny is that for the first reading
# this file does not exist. So it will be generated
# and then re-read by make and used as rules.
include $(subst .tex,.bibliography-make, $(wildcard ./*.tex))

# To build .ps file from a dvi
%.ps: %.dvi
	dvips $< -o $@
	if test -z "$(CREATE_PS)" -a -z "$(CREATE_PS_GZ)"; then rm -f $@; fi;

# To build .ps.gz file with gzip
%.ps.gz: %.ps
	if test -n "$(CREATE_PS)" -a -n "$(CREATE_PS_GZ)"; then  gzip --force -c $^ > $@; fi;
	if test -z "$(CREATE_PS)" -a -n "$(CREATE_PS_GZ)"; then  gzip --force $^; fi;

# OUCH !
# This rule is a bit complex... We have to generate some rules, according to
# your tex file. It depends on the bibliography. If make find the pattern
# BIBLIO_TEX in your tex file, make knows that it have to compile a bib file.
# If not, just running two times latex is ok.
%.bibliography-make: %.tex
	echo "all: tex" > $@.$$$$; \
	if test -n "`cat "${*F}.tex" | grep -e "$(BIBLIO_TEX)" | cut -d "{"  -f 2 | cut -d "}" -f 1`"; then \
	echo -n "$(*F).bbl: " >> $@.$$$$; \
	cat "${*F}.tex" | grep -e "$(BIBLIO_TEX)" | cut -d "{"  -f 2 | cut -d "}" -f 1 | sed "s/$$/.bib/" >> $@.$$$$; \
	if test -n "$(CREATE_PS)" -o -n "$(CREATE_PS_GZ)"; then echo "	$(COMPPS) ${*F}.tex" >> $@.$$$$;  fi; \
	if test -n "$(CREATE_PDF)"; then echo "	$(COMPPDF) ${*F}.tex" >> $@.$$$$;  fi; \
	echo "	$(COMPBIB) ${*F}" >> $@.$$$$; \
	fi; \
	echo -n "${*F}.dvi: " >> $@.$$$$; \
	if test -n "`cat "${*F}.tex" | grep -e "$(BIBLIO_TEX)" | cut -d "{"  -f 2 | cut -d "}" -f 1`"; then \
	echo -n "${*F}.bbl " >> $@.$$$$; \
	fi; \
	echo "${*F}.tex " >> $@.$$$$; \
	echo "	$(COMPPS) $?" >> $@.$$$$; \
	echo "	$(COMPPS) $?" >> $@.$$$$; \
	echo -n "${*F}.pdf: " >> $@.$$$$; \
	if test -n "`cat "${*F}.tex" | grep -e "$(BIBLIO_TEX)" | cut -d "{"  -f 2 | cut -d "}" -f 1`"; then \
	echo -n "${*F}.bbl " >> $@.$$$$; \
	fi; \
	echo "${*F}.tex " >> $@.$$$$; \
	echo "	$(COMPPDF) $?" >> $@.$$$$; \
	echo "	$(COMPPDF) $?" >> $@.$$$$; \
	mv $@.$$$$ $@

# To view the generated pdf

pdf: all tex
	$(PDFVIEW)

# To clean temporary files
%.tex_clean:
	-rm -f $(*F).tns $(*F).log $(*F).aux $(*F).bibliography-make $(*F).blg

# To clean temporary files and target files (.ps.gz and .pdf)
clean:
	$(MAKE) $(subst .tex,.tex_clean, $(wildcard ./*.tex))
	-rm -f $(subst .tex,.bbl, $(wildcard ./*.tex))
	-rm -f $(subst .tex,.dvi, $(wildcard ./*.tex))
	-rm -f $(subst .tex,.ps.gz, $(wildcard ./*.tex))
	-rm -f $(subst .tex,.pdf, $(wildcard ./*.tex))

